﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BookStore.Solution.BLL;
using BookStore.Solution.Entities;
using BookStore.Solution.Utilities;

namespace BookStore.Solution.Presentation
{
    public partial class ucPosition : UserControl
    {
        private PositionManager Positions = new PositionManager();
        private DataTable MyPositionTable = null;
        
        private void LoadData(DataTable table)
        {
            dgvListofPosition.AutoGenerateColumns = false;
            dgvListofPosition.DataSource = table;
            for (int i = 0; i < dgvListofPosition.Columns.Count; i++)
            {
                dgvListofPosition.Columns[i].DataPropertyName = dgvListofPosition.Columns[i].Name;
            }
        }

        public ucPosition()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmPosition frm = new frmPosition();
            frm.ShowDialog();
            if (frm.NewPosition != null)
            {
                MyPositionTable.LoadDataRow(new object[] { frm.NewPosition.PositionID, frm.NewPosition.PositionName }, false);
                LoadData(MyPositionTable);
            }
        }


        private void btnReset_Click(object sender, EventArgs e)
        {
            if (dgvListofPosition.SelectedRows.Count > 0)
            {
                Position entity = new Position();
                entity.PositionID = DataUtil.ToInt(dgvListofPosition.SelectedRows[0].Cells[0].Value);
                entity.PositionName = DataUtil.ToString(dgvListofPosition.SelectedRows[0].Cells[1].Value, "");
                frmPosition frm = new frmPosition(entity);
                frm.ShowDialog();
                if (frm.NewPosition != null)
                {
                    object[] array = { frm.NewPosition.PositionID, frm.NewPosition.PositionName };
                    MyPositionTable.Rows[dgvListofPosition.SelectedRows[0].Index].ItemArray = array;
                    LoadData(MyPositionTable);
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgvListofPosition.SelectedRows.Count > 0)
            {
                if (MessageBox.Show("Bạn có chắc chắn xóa?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    PositionManager manager = new PositionManager();
                    Position entity = new Position();
                    entity.PositionID = DataUtil.ToInt(dgvListofPosition.SelectedRows[0].Cells[0].Value);
                    entity.PositionName = DataUtil.ToString(dgvListofPosition.SelectedRows[0].Cells[1].Value, "");
                    string result = manager.Delete(entity);
                    if (result == "Xóa thành công")
                    {
                        MyPositionTable.Rows.RemoveAt(dgvListofPosition.SelectedRows[0].Index);
                        LoadData(MyPositionTable);
                        MessageBox.Show(result, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                        MessageBox.Show(result, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            btnSearch.Expanded = true;
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            MyPositionTable = Positions.GetAll();
            LoadData(MyPositionTable);
        }

        private bool SetRole()
        {
            bool t = false;
            List<string> roles = AccountManager.GetRoleName();
            btnAdd.Enabled = btnDelete.Enabled = btnUpdate.Enabled = false;
            btnSearch.Enabled = false;
            if (roles.Contains("Thêm, xóa, cập nhật chức vụ"))
            {
                btnAdd.Enabled = btnDelete.Enabled = btnUpdate.Enabled = true;
                t = true;
            }
            if (roles.Contains("Tìm kiếm chức vụ"))
            {
                btnSearch.Enabled = true;
                t = true;
            }
            return t;
        }

        private void ucPosition_Load(object sender, EventArgs e)
        {
            if (SetRole())
            {
                MyPositionTable = Positions.GetAll();
                LoadData(MyPositionTable);
            }
        }

        private void biSearchByID_Click(object sender, EventArgs e)
        {
            frmSearchPosition frm = new frmSearchPosition(SearchPosionType.ID);
            frm.ShowDialog();
            if (frm.ResultTable != null)
            {
                LoadData(frm.ResultTable);
            }
        }

        private void biSearchByName_Click(object sender, EventArgs e)
        {
            frmSearchPosition frm = new frmSearchPosition(SearchPosionType.Name);
            frm.ShowDialog();
            if (frm.ResultTable != null)
            {
                LoadData(frm.ResultTable);
            }
        }

        private void tìmKiếmToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }
    }
}
